import {Comment} from "~/server/models/user";

// 返回所有评论
export default defineEventHandler(async (event) => {
    try {
        const method = event.node.req.method
        if (method === 'GET') {
            // 获取查询参数和分页参数
            const query = getQuery(event);
            const Q = {}
            const currentPage = parseInt(<string>query.currentPage || '1', 10); // 当前页，默认为第1页
            const PerPage = parseInt(<string>query.itemsPerPage || '10', 10); // 每页数量，默认为10

            // 计算跳过的记录数
            const skip = (currentPage - 1) * PerPage;

            // 执行分页查询
            const data = await Comment.find(Q)
                .populate('user')
                .populate('space')
                .skip(skip)
                .limit(PerPage)

            return {
                query,
                currentPage,
                PerPage,
                data
            };
        } else {
            const body = await readBody(event)
            return {
                body,
            }
        }
    } catch (error) {
        return {
            error: error instanceof Error ? error.message : "未知错误",
        }
    }
})